<?php

namespace addons\helper\controller\wxapp\user;

use addons\helper\controller\wxapp\Base;
use fast\Tree;

class Rule extends Base
{
    protected $rulelist = [];
    protected $multiFields = 'ismenu,status';

    protected $config = [
        'model'        => '规则',
        'limit'        => 0,
        'defaultSort'  => 'id',
        'defaultOrder' => 'desc',
        'multi'        => true,
        'field' => [
            'ismenu'     => ['菜单', 'radio', false,  false, '', true, true, true],
            'pid'        => ['父级', 'select', false,  false, '', true, true, true],
            'name'       => ['规则', 'text', false,  false, '', true, true, true],
            'title'      => ['标题', 'text', false,  false, '', true, true, true],
            'weigh'      => ['权重', 'number', false,  false, '', true, true, true],
            'remark'     => ['备注', 'text', false, false, '', true, true, true],
            'createtime' => ['创建时间', 'datetime', false, false, '', true, false, false],
            'updatetime' => ['更新时间', 'datetime', false, false, '', true, false, false],
            'status'     => ['状态', 'select', false, false, '', true, true, true],
        ],
        'fieldContent' => [
            'pid'   => [],
            'ismenu' => [
                '1' => '是',
                '0' => '否'
            ],
            'status' => [
                'normal' => '正常',
                'hidden' => '隐藏'
            ]
        ],
        // [可选]前端表单验证规则
        'rules' => [
            'pid' => [
                'required'    => true
            ],
            'name' => [
                'required'    => true
            ],
            'title' => [
                'required'    => true
            ],
            'weigh' => [
                'required'    => true
            ]
        ], 
        'auth' => [
            'add'  => [
                'name' => '添加',
                'url'  => 'user.rule/add',
            ],
            'edit'  => [
                'name' => '编辑',
                'url'  => 'user.rule/edit',
            ],
            'multi' => [
                'del'  => [
                    'name'        => '删除',
                    'color'       => '#e74c3c',
                    'icon'        => 'fa-trash',
                    'url'         => 'user.rule/del'
                ],
                'hidden'  => [
                    'name'        => '设为隐藏',
                    'color'       => '#f39c12',
                    'icon'        => 'fa-eye-slash',
                    'url'         => 'user.rule/multi',
                    'params'      => 'status=hidden'
                ],
                'normal'  => [
                    'name'        => '设为正常',
                    'color'       => '#18bc9c',
                    'icon'        => 'fa-eye',
                    'url'         => 'user.rule/multi',
                    'params'      => 'status=normal'
                ],
                
            ],
            'more' => [
                'del'  => [
                    'name'  => '删除',
                    'color' => '#e74c3c',
                    'url'   => 'user.rule/del'
                ],
                'hidden'  => [
                    'name'        => '设为隐藏',
                    'color'       => '#f39c12',
                    'url'         => 'user.rule/multi',
                    'params'      => 'status=hidden'
                ],
                'normal'  => [
                    'name'        => '设为正常',
                    'color'       => '#18bc9c',
                    'url'         => 'user.rule/multi',
                    'params'      => 'status=normal'
                ],
            ],
        ],
    ];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\UserRule;
        $this->config['field'] = $this->formatField();
        $this->config['pk'] = $this->model->getPk() ? $this->model->getPk() : 'id';

        // 必须将结果集转换为数组
        $ruleList = collection($this->model->order('weigh', 'desc')->select())->toArray();
        foreach ($ruleList as $k => &$v) {
            $v['title'] = __($v['title']);
            $v['remark'] = __($v['remark']);
        }
        unset($v);
        Tree::instance()->init($ruleList, null, '　');
        $this->rulelist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
        $ruledata = [0 => __('None')];
        foreach ($this->rulelist as $k => &$v) {
            if (!$v['ismenu'])
                continue;
            $ruledata[] = [
                $v['id'] => $v['title']
            ];
        }
        $this->config['fieldContent']['pid']  = $ruledata;
    }

    public function index()
    {
        if ($this->request->isPost()) {
            $list = $this->rulelist;
            $total = count($this->rulelist);

            $result = array("total" => $total, "rows" => $list);

            $data = [
                'tabList'  => null,
                'dataList' => $result,
                'config'   => $this->config
            ];
            $this->success('ok', $data);
        }
    }

    public function del()
    {
        $ids = $this->request->post('ids/a');
        if ($ids) {
            $delIds = [];
            foreach ($ids as $k => $v) {
                $delIds = array_merge($delIds, Tree::instance()->getChildrenIds($v, TRUE));
            }
            $delIds = array_unique($delIds);
            $count = $this->model->where('id', 'in', $delIds)->delete();
            if ($count) {
                $this->success();
            }
        }
        $this->error();
    }
}
